import { getSenceUserList, queryAllKnowLedgeQuestion } from '@/api/knowledgeCompet'
//一些公共方法(获取比赛人员和比赛题目等)，用来判断是否可以计算积分，开始比赛等等
const webscoket = {
  data() {
    return {
      hasUser: false,
      hasQues: false
    }
  },
  computed: {},
  created() {},
  mounted() {},
  methods: {
    setHasUser(val) {
      this.hasUser = val
    },
    setHasQues(val) {
      this.hasQues = val
    },
    async validateStart(showMessage = true) {
      await Promise.all([this.queryAllQuestion(), this.getSenceUserList()])
      showMessage && this.$Message.info('校验完成')
      return this.hasUser && this.hasQues
    },
    getSenceUserList() {
      return new Promise((resolve, reject) => {
        let params = {
          page: 1,
          rows: 10,
          refId: this.competeDetail.id
        }
        getSenceUserList(params)
          .then((data) => {
            if (data.code == 1) {
              this.userTotal = data.attribute.totalCount
              this.setHasUser(true)
              resolve(this.userTotal)
            } else {
              this.setHasUser(false)
              resolve(0)
            }
          })
          .catch((error) => {
            this.setHasUser(false)
            resolve(0)
          })
      })
    },
    queryAllQuestion() {
      return new Promise((resolve, reject) => {
        let queryObj = {
          page: 1,
          rows: 10
        }
        queryAllKnowLedgeQuestion(this.competeDetail.id, queryObj)
          .then((data) => {
            if (data.code === 1) {
              this.quesTotal = data.attribute.totalCount
              this.setHasQues(true)
              resolve(this.quesTotal)
            } else {
              this.setHasQues(false)
              resolve(0)
            }
          })
          .catch((error) => {
            this.setHasQues(false)
            resolve(0)
          })
      })
    }
  },
  destroyed() {}
}

export default webscoket
